﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

using EXCEL = Microsoft.Office.Interop.Excel;

namespace Office
{

    public class ExcelSingle
    {

        #region 定义变量
        /// <summary>
        /// AppLication对象
        /// </summary>
        private EXCEL._Application excelApp = null;

        /// <summary>
        /// Workbook对象
        /// </summary>
        private EXCEL.Workbook excelWorkbook = null;

        /// <summary>
        /// Worksheet对象
        /// </summary>
        private EXCEL.Worksheet excelWorksheet = null;

        /// <summary>
        /// Range对象
        /// </summary>
        private EXCEL.Range excelRange = null;

        /// <summary>
        /// 设置是否启用默认的格式
        /// </summary>
        public bool IsDefaultStyle = false;


        #region 默认格式

        /// <summary>
        /// 字体大小
        /// </summary>
        private int FontSize = 16;

        /// <summary>
        /// 字体名称
        /// </summary>
        private string FontName = "宋体";

        /// <summary>
        /// 字体是否加粗
        /// </summary>
        private bool Bold = false;

        /// <summary>
        /// 边框
        /// </summary>
        private int BorderWidth = 0;

        /// <summary>
        /// 水平对齐
        /// </summary>
        private HAlgin Halgin = HAlgin.HCenter;

        /// <summary>
        /// 垂直对齐
        /// </summary>
        private VAlgin Valgin = VAlgin.VMiddle;

        #endregion


        #region 枚举
        public enum ExcelVersions
        {
            /// <summary>
            /// 2003格式(后缀名xls)
            /// </summary>
            Excel_xls,

            /// <summary>
            /// 2007及其以上格式(后缀名xlsx)
            /// </summary>
            Excel_xlsx,
        }


        public enum HAlgin
        {
            HLeft,

            HCenter,

            HRight,
        }

        public enum VAlgin
        {
            VTop,

            VMiddle,

            VBottom,
        }

        #endregion

        #endregion

        #region 检测和干掉Excel进程
        /// <summary>
        /// 检测是否含有Excel进程
        /// </summary>
        /// <returns></returns>
        public static bool CheckExcelProcess()
        {
            Process[] arrayProcess = System.Diagnostics.Process.GetProcesses();

            foreach (Process P in arrayProcess)
            {
                if (P.ProcessName == "EXCEL")
                {
                    return false;
                }
            }
            return true;
        }


        /// <summary>
        /// 干掉所有EXCEL进程
        /// </summary>
        public static void KillExcelProcess()
        {
            Process[] arrayProcess = System.Diagnostics.Process.GetProcesses();

            foreach (Process P in arrayProcess)
            {
                if (P.ProcessName == "EXCEL")
                {
                    P.Kill();
                }
            }
        }
        #endregion

        public ExcelSingle()
        {


        }


        #region 创建一个Excel文件
        /// <summary>
        /// 创建一个Excel文件
        /// </summary>
        /// <param name="TemplatePath">如果需要模板的话，根据模板路径创建(模板的绝对路径)</param>
        public void CreateExcel(string TemplatePath = null)
        {
            if (excelApp == null)
            {
                excelApp = new EXCEL.Application();
            }

            if (TemplatePath != null)
            {
                excelWorkbook = excelApp.Workbooks.Add(TemplatePath);
            }
            else
            {
                excelWorkbook = excelApp.Workbooks.Add();
            }
        }
        #endregion

        #region  打开一个Excel文件
        /// <summary>
        /// 打开一个Excel文件
        /// </summary>
        /// <param name="TemplatePath">打开文件的绝对路径</param>
        public void OpenExcel(string FilenamePath = null)
        {
            if (excelApp == null)
            {
                excelApp = new EXCEL.Application();
            }

            excelWorkbook = excelApp.Workbooks.Open(FilenamePath);
        }

        #endregion


        #region 打开一个Excel工作表,没有名字的话创建
        /// <summary>
        /// 打开一个Excel工作表,没有名字的话创建
        /// </summary>
        /// <param name="WorksheetName">工作表的名称，缺省的话，默认打开第一个</param>
        public void OpenExcelWorksheet(string WorksheetName = null)
        {

            EXCEL.Sheets excelWorksheets = excelWorkbook.Worksheets;


            try
            {

                if (WorksheetName == null)
                {
                    if (excelWorksheets.Count != 0)
                    {
                        excelWorksheet = (EXCEL.Worksheet)excelWorksheets[1];

                        return;
                    }
                }


                try
                {
                    excelWorksheet = (EXCEL.Worksheet)excelWorksheets[WorksheetName];
                }
                catch (Exception ex)
                {
                    excelWorksheet = (EXCEL.Worksheet)excelWorksheets.Add();
                    excelWorksheet.Name = WorksheetName;
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("Excel创建工作表失败\r\n" + ex.Message);
                CloseAllExcel();
                throw new Exception("Excel创建工作表失败");
            }

        }
        #endregion

        #region 获得excelRange对象
        /// <summary>
        /// 获得excelRange对象
        /// </summary>
        /// <param name="StartCell">起始位置</param>
        /// <param name="StopCell">终点位置(为null则和起始位置一样)</param>
        /// <returns></returns>
        public void GetexcelRange(string StartCell, string StopCell)
        {
            try
            {

                if (string.IsNullOrEmpty(StartCell))
                {
                    return;
                }

                if (string.IsNullOrEmpty(StopCell))
                {
                    StopCell = StartCell;
                }

                if (excelWorksheet == null)
                {
                    OpenExcelWorksheet();
                }

                if (excelWorksheet != null)
                {
                    excelRange = excelWorksheet.get_Range(StartCell, StopCell);
                    return;
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("创建excelRange错误\r\n" + ex.Message);
                return;
            }
        }
        #endregion

        #region 获得excelCharacters对象

        /// <summary>
        /// 获得excelCharacters对象
        /// </summary>
        /// <param name="StartChar">起始位置</param>
        /// <param name="LengthChar">长度(小于等于0则和起始位置一样)</param>
        /// <returns></returns>
        public EXCEL.Characters GetexcelCharacters(int StartChar, int LengthChar)
        {
            if (excelRange == null)
            {
                return null;
            }

            EXCEL.Characters excelCharacters = null;


            try
            {

                if (StartChar < 0)
                {
                    return null;
                }

                if (LengthChar <= 0)
                {
                    excelCharacters = excelRange.get_Characters(StartChar, excelRange.Text.ToString().Length - StartChar);

                }
                else
                {
                    excelCharacters = excelRange.get_Characters(StartChar, LengthChar);
                }

                return excelCharacters;
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("获得excelCharacters错误\r\n" + ex.Message);
                return null;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="StartChar"></param>
        /// <param name="LengthChar"></param>
        /// <returns></returns>

        /// <summary>
        /// 获得excelCharacters对象
        /// </summary>
        /// <param name="StartCell">Range起始位置</param>
        /// <param name="StopCell">Range终点位置(为空则默认和起始位置一样)</param>
        /// <param name="StartChar">起始位置</param>
        /// <param name="LengthChar">长度(小于等于0则和起始位置一样)</param>
        /// <returns></returns>
        private EXCEL.Characters GetexcelCharacters(string StartCell, string StopCell, int StartChar, int LengthChar)
        {
            GetexcelRange(StartCell, StopCell);

            EXCEL.Characters excelCharacters = null;


            try
            {

                if (StartChar < 0)
                {
                    return null;
                }

                if (LengthChar <= 0)
                {
                    excelCharacters = excelRange.get_Characters(StartChar, excelRange.Text.ToString().Length - StartChar);

                }
                else
                {
                    excelCharacters = excelRange.get_Characters(StartChar, LengthChar);
                }

                return excelCharacters;
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("获得excelCharacters错误\r\n" + ex.Message);
                return null;
            }
        }
        #endregion




        #region 写数据

        public string CaptionName
        {
            get
            {
                return excelApp.Caption;
            }

            set
            {
                excelApp.Caption = value;
            }
        }

        #endregion


        #region 操作Excel

        #region 合并单元格
        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="Cell1">起始位置</param>
        /// <param name="Cell2">结束位置</param>
        /// <param name="mergeCells">合并/拆分</param>
        /// <returns></returns>
        public bool mergeCells(string Cell1, string Cell2, bool mergeCells = true)
        {

            try
            {
                GetexcelRange(Cell1, Cell2);

                excelRange = excelWorksheet.get_Range(Cell1, Cell2);

                excelRange.MergeCells = mergeCells;

                return true;
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("操作单元格失败\r\n" + ex.Message);
                return false;
            }


        }
        #endregion


        #region 读数据

        /// <summary>
        /// 读数据
        /// </summary>
        /// <param name="Cell1">起始坐标</param>
        /// <param name="Cell2">结束坐标</param>
        /// <param name="DataContent">数据内容</param>
        /// <returns>bool</returns>
        public object GetData(string Cell1, string Cell2)
        {

            try
            {

                GetexcelRange(Cell1, Cell2);

                if (excelRange != null)
                {
                    return excelRange.get_Value();
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("读取excel数据错误\r\n" + ex.Message);
                return null;
            }
        }
        #endregion


        #region 写数据

        /// <summary>
        /// 写数据
        /// </summary>
        /// <param name="Cell1">起始坐标</param>
        /// <param name="Cell2">结束坐标</param>
        /// <param name="DataContent">数据内容</param>
        /// <returns>bool</returns>
        public bool WriteData(string Cell1, string Cell2, object DataContent)
        {

            if (DataContent == null)
            {
                return true;
            }
            else
            {
                if (string.IsNullOrEmpty(DataContent.ToString()))
                {
                    return true;
                }
            }

            try
            {

                if (excelWorksheet == null)
                {
                    OpenExcelWorksheet();
                }


                if (Cell2 == null)
                {
                    Cell2 = Cell1;
                }

                excelRange = excelWorksheet.get_Range(Cell1, Cell2);



                excelRange.set_Value(EXCEL.XlRangeValueDataType.xlRangeValueDefault, DataContent);


                if (IsDefaultStyle)
                {
                    DefaultStyle();
                }


                return true;
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("写入excel数据错误\r\n" + ex.Message);
                return false;
            }
        }
        #endregion

        #region 设置格式


        /// <summary>
        /// 设置编辑的区域
        /// </summary>
        /// <param name="Range">编辑区域</param>
        /// <param name="FontSize">字号</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="Bold">字体是否加粗</param>
        /// <param name="Border">显示边框</param>
        public void setStyle(string StartCell, string StopCell, int FontSize, string FontName, bool Bold, bool Border, HAlgin Halgin = HAlgin.HCenter, VAlgin Valgin = VAlgin.VMiddle)
        {
            try
            {
                excelRange = excelWorksheet.get_Range(StartCell, StopCell);

                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式的位置出错\r\n" + ex.Message);
                return;
            }

            //设置字号的大小
            if (FontSize > 0)
            {
                excelRange.Font.Size = FontSize;
            }

            //设置字体
            if (!string.IsNullOrEmpty(FontName))
            {
                excelRange.Font.Name = FontName;
            }

            //对齐方式
            excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignCenter;
            excelRange.VerticalAlignment = EXCEL.XlVAlign.xlVAlignCenter;

            //是否加粗
            excelRange.Font.Bold = Bold;

            //边框
            if (Border)
            {
                excelRange.Borders.LineStyle = 1;
            }

            excelRange.Rows.AutoFit();
            excelRange.WrapText = true;


        }

        #region 字体加粗

        /// <summary>
        /// 设置字体是否加粗
        /// </summary>
        /// <param name="StartCell">起始位置</param>
        /// <param name="StopCell">结束位置</param>
        /// <param name="Bold">是否加粗</param>
        public void SetFontBold(string StartCell, string StopCell, bool Bold)
        {
            GetexcelRange(StartCell, StopCell);

            try
            {
                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式加粗出错\r\n" + ex.Message);
                return;
            }

            excelRange.Font.Bold = Bold;

        }

        /// <summary>
        /// 设置字体是否加粗
        /// </summary>
        public void SetFontBold(bool Bold)
        {
            try
            {
                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式加粗出错\r\n" + ex.Message);
                return;
            }

            excelRange.Font.Bold = Bold;
        }


        /// <summary>
        /// 设置字体是否加粗
        /// </summary>
        /// <param name="StartCell">Range起始位置</param>
        /// <param name="StopCell">Range结束位置(为null则默认和起始位置一样)</param>
        /// <param name="StartChar">Characters中字符串的起始位置</param>
        /// <param name="StopChar">CCharacters长度(小于等于0则默认为字符串的终点)</param>
        /// <param name="Bold">加粗</param>
        public void SetFontBold(string StartCell, string StopCell, int StartChar, int LengthChar, bool Bold)
        {

            try
            {
                GetexcelRange(StartCell, StopCell);

                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式加粗出错\r\n" + ex.Message);
                return;
            }

            EXCEL.Characters excelCharacters = GetexcelCharacters(StartChar, LengthChar);

            if (excelCharacters != null)
            {
                excelCharacters.Font.Bold = Bold;
            }
        }

        /// <summary>
        /// 设置字体是否加粗
        /// </summary>
        /// <param name="StartChar">Characters起始位置</param>
        /// <param name="LengthChar">Characters长度(小于等于0则默认为字符串的终点) </param>
        /// <param name="Bold">加粗</param>
        public void SetFontBold(int StartChar, int LengthChar, bool Bold)
        {
            try
            {
                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式加粗出错\r\n" + ex.Message);
                return;
            }

            EXCEL.Characters excelCharacters = GetexcelCharacters(StartChar, LengthChar);

            if (excelCharacters != null)
            {
                excelCharacters.Font.Bold = Bold;
            }
        }
        #endregion


        #region 字体下划线

        /// <summary>
        /// 设置字体是否下划线
        /// </summary>
        /// <param name="StartCell">range起始位置</param>
        /// <param name="StopCell">range结束位置</param>
        /// <param name="Underline">是否下划线</param>
        public void SetFontUnderline(string StartCell, string StopCell, bool Underline)
        {

            try
            {


                excelRange = excelWorksheet.get_Range(StartCell, StopCell);

                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式下划线出错\r\n" + ex.Message);
                return;
            }

            if (Underline)
            {
                excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleSingle;
            }
            else
            {
                excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleNone;
            }

        }

        /// <summary>
        /// 设置字体是否下划线
        /// </summary>
        public void SetFontUnderline(bool Underline)
        {
            try
            {
                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式加粗出错\r\n" + ex.Message);
                return;
            }

            if (Underline)
            {
                excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleSingle;
            }
            else
            {
                excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleNone;
            }
        }


        /// <summary>
        /// 设置字体是否下划线
        /// </summary>
        /// <param name="StartCell">Range起始位置</param>
        /// <param name="StopCell">Range结束位置(为null则默认和起始位置一样)</param>
        /// <param name="StartChar">Characters中字符串的起始位置</param>
        /// <param name="StopChar">CCharacters长度(小于等于0则默认为字符串的终点)</param>
        /// <param name="Underline">下划线</param>
        public void SetFontUnderline(string StartCell, string StopCell, int StartChar, int LengthChar, bool Underline)
        {
            try
            {
                GetexcelRange(StartCell, StopCell);

                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式加粗出错\r\n" + ex.Message);
                return;
            }

            EXCEL.Characters excelCharacters = GetexcelCharacters(StartChar, LengthChar);

            if (excelCharacters != null)
            {
                if (Underline)
                {
                    excelCharacters.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleSingle;
                }
                else
                {
                    excelCharacters.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleNone;
                }
            }
        }

        /// <summary>
        /// 设置字体是否下划线
        /// </summary>
        /// <param name="StartChar">Characters起始位置</param>
        /// <param name="LengthChar">Characters长度(小于等于0则默认为字符串的终点) </param>
        /// <param name="Underline">加粗</param>
        public void SetFontUnderline(int StartChar, int LengthChar, bool Underline)
        {
            try
            {
                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置格式格式加粗出错\r\n" + ex.Message);
                return;
            }

            EXCEL.Characters excelCharacters = GetexcelCharacters(StartChar, LengthChar);


            if (excelCharacters != null)
            {
                if (Underline)
                {
                    excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleSingle;
                }
                else
                {
                    excelRange.Font.Underline = EXCEL.XlUnderlineStyle.xlUnderlineStyleNone;
                }
            }
        }
        #endregion

        #region 字体加粗
        #endregion

        #region 设置默认的格式
        /// <summary>
        /// 设置默认的格式
        /// </summary>
        /// <param name="Range">编辑区域</param>
        /// <param name="FontSize">字号</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="Bold">字体是否加粗</param>
        /// <param name="Border">显示边框</param>
        public void setDefalutStyle(int FontSize = -1, string FontName = null, bool Bold = false, int BorderWidth = -1, HAlgin Halgin = HAlgin.HCenter, VAlgin Valgin = VAlgin.VMiddle)
        {

            if (FontSize > 0)
            {
                this.FontSize = FontSize;
            }

            if (string.IsNullOrEmpty(FontName))
            {
                this.FontName = FontName;
            }


            this.Bold = Bold;

            if (BorderWidth >= 0)
            {
                this.BorderWidth = BorderWidth;
            }


            this.Halgin = Halgin;

            this.Valgin = Valgin;

        }
        #endregion

        #region 设置边框
        /// <summary>
        /// 设置边框
        /// </summary>
        /// <param name="BorderWidth"></param>
        public void SetBorder(int BorderWidth)
        {
            if (BorderWidth >= 0)
            {
                excelRange.Borders.LineStyle = BorderWidth;
            }
            else
            {
                excelRange.Borders.LineStyle = this.BorderWidth;
            }
        }

        #endregion


        #region 宽

        /// <summary>
        /// 设置宽
        /// </summary>
        /// <param name="Width">宽度</param>
        public void SetWidth(double Width)
        {
            if (excelRange != null)
            {
                excelRange.ColumnWidth = Width;

                excelRange.AutoFit();

                excelRange.WrapText = true;
            }
        }

        /// <summary>
        /// 设置宽
        /// </summary>
        /// <param name="colums">选中的列,可以为数字,也可以为字母的组合</param>
        /// <param name="Width">宽度</param>
        public void SetWidth(string colums, double Width)
        {
            int result = 0;


            try
            {

                if (int.TryParse(colums, out result))
                {
                    excelRange = (EXCEL.Range)excelWorksheet.Columns[result];

                }

                else
                {
                    excelRange = (EXCEL.Range)excelWorksheet.Columns[colums.ToUpper()];
                }


                if (excelRange != null)
                {
                    excelRange.AutoFit();

                    excelRange.WrapText = true;

                    excelRange.ColumnWidth = Width;

                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置宽出错");
            }
        }

        /// <summary>
        /// 获得宽
        /// </summary>
        /// <returns>宽度</returns>
        public double GetWidth()
        {
            try
            {

                if (excelRange != null)
                {
                    object obj = excelRange.ColumnWidth;

                    if (obj != null)
                    {
                        return double.Parse(obj.ToString());
                    }

                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("获取宽出错\r\n" + ex.Message);
            }

            return 0;
        }

        /// <summary>
        /// 获得宽
        /// </summary>
        /// <param name="colums">选中的列,可以为数字,也可以为字母的组合</param>
        /// <returns>宽度</returns>
        public double GetWidth(string colums)
        {
            int result = 0;

            try
            {

                if (int.TryParse(colums, out result))
                {
                    excelRange = (EXCEL.Range)excelWorksheet.Columns[result];

                }

                else
                {
                    excelRange = (EXCEL.Range)excelWorksheet.Columns[colums.ToUpper()];
                }

                if (excelRange != null)
                {
                    object obj = excelRange.ColumnWidth;

                    if (obj != null)
                    {
                        return double.Parse(obj.ToString());
                    }

                }


            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("获取宽出错\r\n" + ex.Message);
            }

            return 0;
        }





        #endregion


        #region 高

        /// <summary>
        /// 设置高
        /// </summary>
        /// <param name="Width">高度</param>
        public void SetHeight(double Height)
        {
            if (excelRange != null)
            {

                excelRange.AutoFit();

                excelRange.WrapText = true;

                excelRange.RowHeight = Height;


            }
        }

        /// <summary>
        /// 设置高
        /// </summary>
        /// <param name="cells">选中的行,只能为整数</param>
        /// <param name="Width">高度</param>
        public void SetHeight(int cells, double Height)
        {



            if (cells <= 0)
            {
                return;
            }

            try
            {
                excelRange = (EXCEL.Range)excelWorksheet.Rows[cells];




                if (excelRange != null)
                {

                    excelRange.AutoFit();

                    excelRange.WrapText = true;

                    excelRange.RowHeight = Height; ;


                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置高出错" + ex.Message);
            }
        }

        /// <summary>
        /// 获得高
        /// </summary>
        /// <returns>高度</returns>
        public double GetHeight()
        {
            try
            {

                if (excelRange != null)
                {
                    object obj = excelRange.RowHeight;

                    if (obj != null)
                    {
                        return double.Parse(obj.ToString());
                    }

                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("获取高出错\r\n" + ex.Message);
            }

            return 0;
        }

        /// <summary>
        /// 获得高
        /// </summary>
        /// <param name="colums">选中的行,只能为整数</param>
        /// <returns>高度</returns>
        public double GetHeight(int cells)
        {
            if (cells <= 0)
            {
                return 0;
            }

            try
            {
                excelRange = (EXCEL.Range)excelWorksheet.Rows[cells];




                if (excelRange != null)
                {

                    object obj = excelRange.RowHeight;

                    if (obj != null)
                    {
                        return double.Parse(obj.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置高出错" + ex.Message);
            }

            return 0;
        }





        #endregion

        #region 设置对齐方式


        /// <summary>
        /// 设置对齐方式
        /// </summary>
        /// <param name="Halgin">水平对齐</param>
        /// <param name="Valgin">垂直对齐</param>
        public void SetAlgin(HAlgin Halgin = HAlgin.HCenter, VAlgin Valgin = VAlgin.VMiddle)
        {
            if (excelRange != null)
            {

                if (this.Halgin == HAlgin.HLeft)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignLeft;
                }

                if (this.Halgin == HAlgin.HCenter)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignCenter;
                }

                if (this.Halgin == HAlgin.HRight)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignRight;
                }


                if (this.Valgin == VAlgin.VTop)
                {
                    excelRange.VerticalAlignment = EXCEL.XlVAlign.xlVAlignTop;
                }

                if (this.Valgin == VAlgin.VMiddle)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignCenter;
                }

                if (this.Valgin == VAlgin.VBottom)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignBottom;
                }


            }
        }


        /// <summary>
        /// 设置对齐方式
        /// </summary>
        /// <param name="StartCell">Range起始位置</param>
        /// <param name="StopCell">Range结束位置(为null则默认和起始位置一样)</param>
        /// <param name="Halgin">水平对齐</param>
        /// <param name="Valgin">垂直对齐</param>
        public void SetAlgin(string StartCell, string StopCell, HAlgin Halgin = HAlgin.HCenter, VAlgin Valgin = VAlgin.VMiddle)
        {

            GetexcelRange(StartCell, StopCell);

            if (excelRange != null)
            {

                if (this.Halgin == HAlgin.HLeft)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignLeft;
                }

                if (this.Halgin == HAlgin.HCenter)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignCenter;
                }

                if (this.Halgin == HAlgin.HRight)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignRight;
                }


                if (this.Valgin == VAlgin.VTop)
                {
                    excelRange.VerticalAlignment = EXCEL.XlVAlign.xlVAlignTop;
                }

                if (this.Valgin == VAlgin.VMiddle)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignCenter;
                }

                if (this.Valgin == VAlgin.VBottom)
                {
                    excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignBottom;
                }

            }
        }

        #endregion

        #region 设置高
        #endregion

        #region 设置高
        #endregion

        #region 设置高
        #endregion


        /// <summary>
        /// 执行默认格式
        /// </summary>
        private void DefaultStyle()
        {
            try
            {

                if (excelRange == null)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("设置默认格式的位置出错\r\n" + ex.Message);
                return;
            }

            //设置字号的大小
            excelRange.Font.Size = this.FontSize;

            //设置字体
            excelRange.Font.Name = this.FontName;

            //对齐方式

            if (this.Halgin == HAlgin.HLeft)
            {
                excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignLeft;
            }

            if (this.Halgin == HAlgin.HCenter)
            {
                excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignCenter;
            }

            if (this.Halgin == HAlgin.HRight)
            {
                excelRange.HorizontalAlignment = EXCEL.XlHAlign.xlHAlignRight;
            }


            if (this.Valgin == VAlgin.VTop)
            {
                excelRange.VerticalAlignment = EXCEL.XlVAlign.xlVAlignTop;
            }

            if (this.Valgin == VAlgin.VMiddle)
            {
                excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignCenter;
            }

            if (this.Valgin == VAlgin.VBottom)
            {
                excelRange.HorizontalAlignment = EXCEL.XlVAlign.xlVAlignBottom;
            }



            //是否加粗
            excelRange.Font.Bold = this.Bold;

            excelRange.Borders.LineStyle = this.BorderWidth;



            excelRange.Rows.AutoFit();
            excelRange.WrapText = true;
        }



        #endregion

        #endregion







        #region 保存Excel工作表
        /// <summary>
        /// 保存Excel工作表
        /// </summary>
        /// <param name="SaveAsExcelFilePath">保存Excel文件的绝对路径</param>
        /// <param name="excelVersion">保存Excel文件的类型,默认07以上类型(03都快淘汰了，亲)</param>
        public void SaveAsExcelFile(string SaveAsExcelFilePath, Excel.ExcelVersions excelVersion = Excel.ExcelVersions.Excel_xlsx)
        {
            try
            {
                if (excelWorkbook == null)
                {
                    throw new Exception("当前连实例都木有创建，保存你妹啊");
                }

                FileInfo file = null;

                try
                {
                    file = new FileInfo(SaveAsExcelFilePath);
                }
                catch
                {
                    throw new Exception("存储地址不正确");
                }

                string DirectoryPath = null;

                string fileName = null;

                string FileName_prefix = null;

                string FileName_suffix = null;


                if(file!=null&&!string.IsNullOrEmpty(file.DirectoryName))
                {
                    char c= file.DirectoryName.ToCharArray()[file.DirectoryName.Length - 1];
                    if (c != '\\')
                    {
                        DirectoryPath = file.DirectoryName + "\\";
                    }
                    else
                    {
                        DirectoryPath = file.DirectoryName;
                    }
                }
                

                fileName = file.Name;

                try
                {

                    FileName_prefix = file.Name.Substring(0, file.Name.LastIndexOf("."));
                }
                catch (Exception ex)
                {
                    FileName_prefix = fileName;
                }

                FileName_suffix = file.Extension;




                if (excelVersion == Excel.ExcelVersions.Excel_xlsx)
                {

                    if (FileName_suffix != ".xlsx")
                    {
                        FileName_prefix = FileName_prefix + FileName_suffix;

                        fileName = FileName_prefix + ".xlsx";
                    }


                    int count = 0;

                    SaveAsExcelFilePath = DirectoryPath + fileName;

                    if (File.Exists(SaveAsExcelFilePath))
                    {

                        do
                        {
                            count++;
                            SaveAsExcelFilePath = DirectoryPath + FileName_prefix + string.Format("({0:0000})", count) + ".xlsx";
                        }
                        while (File.Exists(SaveAsExcelFilePath));
                    }

                    if (!Directory.Exists(DirectoryPath))//   .Exists(SaveAsExcelFilePath))
                    {
                        Directory.CreateDirectory(DirectoryPath);
                    }


                    excelWorkbook.SaveAs(SaveAsExcelFilePath, EXCEL.XlFileFormat.xlWorkbookDefault);
                }
                else
                {

                    if (FileName_suffix != ".xls")
                    {
                        FileName_prefix = FileName_prefix + FileName_suffix;

                        fileName = FileName_prefix + ".xls";
                    }


                    int count = 0;

                    SaveAsExcelFilePath = DirectoryPath + fileName;

                    if (File.Exists(SaveAsExcelFilePath))
                    {

                        do
                        {
                            count++;
                            SaveAsExcelFilePath = DirectoryPath + FileName_prefix + string.Format("({0:0000})", count) + ".xls";
                        }
                        while (File.Exists(SaveAsExcelFilePath));
                    }

                    if (!Directory.Exists(DirectoryPath))//   .Exists(SaveAsExcelFilePath))
                    {
                        Directory.CreateDirectory(DirectoryPath);
                    }


                    excelWorkbook.SaveAs(SaveAsExcelFilePath, EXCEL.XlFileFormat.xlWorkbookNormal);
                }


            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("保存失败\r\n" + ex.Message);
                CloseAllExcel();
                throw new Exception("保存失败");
            }
        }


        /// <summary>
        /// 保存Excel工作表
        /// </summary>
        /// <param name="SaveAsExcelFilePath">保存Excel文件的绝对路径</param>
        public void SaveExcelFile()
        {
            try
            {
                if (excelWorkbook == null)
                {
                    throw new Exception("当前连实例都木有创建，保存你妹啊");
                }

                excelWorkbook.Save();

            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("保存失败\r\n" + ex.Message);
                CloseAllExcel();
                throw new Exception("保存失败");
            }
        }

        #endregion



        #region 关闭本对象的Excel但不释放Application对象
        /// <summary>
        /// 关闭本对象的Excel但不释放Application对象
        /// </summary>
        public void CloseExcel(bool saveChange)
        {
            try
            {
                excelWorkbook.Close(saveChange);
                
                excelWorkbook = null;
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("关闭Excel失败");
                CloseAllExcel();
                throw new Exception("关闭Excel失败");
            }
        }
        #endregion


        #region 关闭所有Excel对象
        /// <summary>
        /// 关闭所有Excel对象
        /// </summary>
        public void CloseAllExcel()
        {
            try
            {
                if (excelApp != null)
                {
                    //EXCEL.Workbooks workbooks = excelApp.Workbooks;

                    //for (int i = 1; i <= workbooks.Count; i++)
                    //{
                    //    workbooks[i].Close();
                    //}


                    //销毁APP
                    excelApp.Quit();
                    excelApp = null;
                }
            }
            catch (Exception ex)
            {
                //new ErrorLog.LogFileTxt().WriteFile("关闭所有Excel失败");
                throw new Exception("关闭所有Excel失败");
            }
        }

        #endregion




    }
}
